﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;

public partial class Admin_Admin_Login : HoWave_Root, IRequiresSessionState
{
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
	protected global_asax applicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	private void AutoNoSaler()
	{
		try
		{
			this.conn.Execute("update HLShop_Product set EnableSale=0  where EndDate <= getdate()");
		}
		catch (Exception ex)
		{
         
		}
	}
	protected void LoginSubmit_Click1(object sender, EventArgs e)
	{
		this.AutoNoSaler();
		OnlineAdmin onlineAdmin = new OnlineAdmin();
		string text = "";
		if (!(this.AdminUserName.Text == "") && !(this.AdminPassword.Text == ""))
		{
			if (this.CheckValidateCode(this.CheckCode.Text))
			{
				string name = base.Server.HtmlEncode(this.AdminUserName.Text);
				string pwd = this.Md5(base.Server.HtmlEncode(this.AdminPassword.Text));
				if (onlineAdmin.LoginAdmin(name, pwd))
				{
					LogBll logBll = new LogBll(this.conn);
					try
					{
						this.conn.Execute("Update HL_Admin Set LoginTimes=LoginTimes+1,LastLogin=LoginDate,LastLoginIP=LoginIP Where Admin_ID=" 

+ onlineAdmin.AdminID);
						IDbDataParameter[] parameters = new IDbDataParameter[]
						{
							this.conn.CreateParameter("@LoginDate", DbType.DateTime, DateTime.Now),
							this.conn.CreateParameter("@LoginIP", DbType.String, Fetch.UserIp)
						};
						this.conn.Execute("Update HL_Admin Set LoginDate = @LoginDate, LoginIP = @LoginIP Where Admin_ID=" + onlineAdmin.AdminID, parameters);
					}
					catch (Exception ex)
					{
						base.Response.Write(ex.Message.ToString());
						onlineAdmin.LoginOut();
						logBll.AddLog("Login", Fetch.CurrentUrl, ex.Message.ToString(), DateTime.Now, onlineAdmin.AdminName, Fetch.UserIp, Fetch.webDomain, 1, "Login");
						base.Response.End();
					}
					logBll.AddLog("Login", Fetch.CurrentUrl, Language.GetAdminMsg("Login/LoginOK"), DateTime.Now, onlineAdmin.AdminName, 

Fetch.UserIp, Fetch.webDomain, 0, "Login");
					base.Response.Redirect("admin_index.aspx");
				}
				else
				{
					text = onlineAdmin.ErrStr;
					LogBll logBll = new LogBll(this.conn);
					logBll.AddLog("Login", Fetch.CurrentUrl, text, DateTime.Now, onlineAdmin.AdminName, Fetch.UserIp, Fetch.webDomain, 1, "Login");
				}
			}
			else
			{
				text = Language.GetAdminMsg("Login/CodeErr");
			}
			this.err_tips.Text = text;
		}
	}
}